package com.LeeCode;

/**
 * 统计二叉树中好节点的数目
 */

public class Code1448 {
    public static void main(String[] args) {
        Integer[] arr = {3, 3, null, 4, 2};
        TreeNode root = Utils.buildTree(arr);
        System.out.println(new Code1448().goodNodes(root));
    }

    private int ans = 0;

    public int goodNodes(TreeNode root) {
        dfs(root, Integer.MIN_VALUE);
        return ans;
    }

    private void dfs(TreeNode root, int currentMax) {
        if (root == null) return;
        if (root.val >= currentMax) ans++;
        currentMax = Math.max(root.val, currentMax);
        dfs(root.left, currentMax);
        dfs(root.right, currentMax);
    }
}
